package org.aphelie.torg.ruler.internal.core;

import java.lang.reflect.InvocationTargetException;

import org.aphelie.torg.ruler.core.character.ICharacterModelBase;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;



public class TGRCore extends Plugin {

    public static final String PLUGIN_ID = "org.aphelie.torg.ruler.core"; //$NON-NLS-1$
    
    public static final IPath REQUIRED_PLUGINS_CONTAINER_PATH = new Path(PLUGIN_ID + ".requiredPlugins"); //$NON-NLS-1$
    public static final IPath JAVA_SEARCH_CONTAINER_PATH = new Path(PLUGIN_ID + ".externalJavaSearch"); //$NON-NLS-1$
    //public static final IPath JRE_CONTAINER_PATH = new Path(JavaRuntime.JRE_CONTAINER);

    public static final String BINARY_PROJECT_VALUE = "binary"; //$NON-NLS-1$
    public static final String BINARY_REPOSITORY_PROVIDER = PLUGIN_ID + "." + "BinaryRepositoryProvider"; //$NON-NLS-1$ //$NON-NLS-2$

    public static final QualifiedName EXTERNAL_PROJECT_PROPERTY = new QualifiedName(PLUGIN_ID, "imported"); //$NON-NLS-1$
    public static final QualifiedName TOUCH_PROJECT = new QualifiedName(PLUGIN_ID, "TOUCH_PROJECT"); //$NON-NLS-1$

    public static final QualifiedName SCHEMA_PREVIEW_FILE = new QualifiedName(PLUGIN_ID, "SCHEMA_PREVIEW_FILE"); //$NON-NLS-1$
    
    
    private static TGRCore fInstance;
    
    
    
    private static ICharacterModelBase[] registryCharacters;
    
    
    
    private CharacterModelManager fModelManager;
    
    
    private static TGRPreferencesManager fPreferenceManager;
    
    public static TGRCore getDefault() {
        return fInstance;
    }
    
    public synchronized TGRPreferencesManager getPreferencesManager() {
        if (fPreferenceManager == null) {
            fPreferenceManager = new TGRPreferencesManager(PLUGIN_ID);
        }
        return fPreferenceManager;
    }

    public static IWorkspace getWorkspace() {
        return ResourcesPlugin.getWorkspace();
    }

    public static void log(IStatus status) {
        if (status != null)
            ResourcesPlugin.getPlugin().getLog().log(status);
    }

    public static void log(Throwable e) {
        if (e instanceof InvocationTargetException)
            e = ((InvocationTargetException) e).getTargetException();
        IStatus status = null;
        if (e instanceof CoreException) {
            status = ((CoreException) e).getStatus();
        } else if (e.getMessage() != null) {
            status = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, e.getMessage(), e);
        }
        log(status);
    }

    public static void logErrorMessage(String message) {
        log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, message, null));
    }

    public static void logException(Throwable e) {
        logException(e, null);
    }

    public static void logException(Throwable e, String message) {
        if (e instanceof InvocationTargetException) {
            e = ((InvocationTargetException) e).getTargetException();
        }
        IStatus status = null;
        if (e instanceof CoreException)
            status = ((CoreException) e).getStatus();
        else {
            if (message == null)
                message = e.getMessage();
            if (message == null)
                message = e.toString();
            status = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, e);
        }
        log(status);
    }
    
    
    
    public TGRCore() {
        fInstance = this;
    }
    
    
    public CharacterModelManager getModelManager() {
        if (fModelManager == null) {
            fModelManager = new CharacterModelManager();
            /** TODO
            // when initializing plug-in models, create the extension registry so it can track relevant (ModelChange) events.
            if (fExtensionRegistry == null)
                getExtensionsRegistry();
             */
        }
        return fModelManager;
    }
    
    
    
    
    
}
